package cn.zcf.org.controller;

import cn.zcf.basic.utils.JsonResult;
import cn.zcf.basic.utils.PageList;

import cn.zcf.org.domain.Emp;
import cn.zcf.org.query.EmpQuery;
import cn.zcf.org.service.IEmpService;
import cn.zcf.system.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmpController {
    @Autowired
    private IEmpService service;
    @GetMapping
    @ApiOperation(value = "查询全部" )

    public List<Emp> loadAll(){
        return service.loadAll();
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Emp loadById(@PathVariable("id") Long id){
        return service.loadById(id);
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    /*@PreAuthorize(name = "员工删除",sn = "emp:delete")*/
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            service.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }
    @PutMapping
    @ApiOperation(value = "新增或修改" )
    public JsonResult addOrUpt(@RequestBody Emp emp){
        try {
            if(emp.getId()==null){
                service.add(emp);
            }
            else {
                service.update(emp);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Emp> pageQuery(@RequestBody EmpQuery query){
        try {
            return service.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Emp>();
        }

    }
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            service.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }
    @GetMapping("/list/{shopId}")

    public List<Emp> loadByShopId(@PathVariable("shopId") Long shopId ){
        return service.loadByShopId(shopId);
    }
}
